import 'package:dio/dio.dart';
import 'package:my_shop/util/app_sp.dart';
import 'package:my_shop/util/device_info_util.dart';

/// created by devin
/// 2025/5/10 23:34
/// description: 
class TokenInterceptor extends InterceptorsWrapper {

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    _addDefaultHeaders(options);
    super.onRequest(options, handler);
  }

  /// 添加默认请求头
  void _addDefaultHeaders(RequestOptions options) {
    final token = AppSp.getAccessToken();
    options.headers['deviceId'] = DeviceInfoUtil.deviceId;
    options.headers['deviceInfo'] = DeviceInfoUtil.deviceInfo;
    if (token.isNotEmpty) {
      options.headers['Authorization'] = "Bearer $token";
    }
  }
}